// Tierless
// Copyright © 2010 Alphora
// This file is licensed under a modified BSD-license (see tierless_license.txt).
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;

using Tierless.Framework.Entities;

namespace Tierless.Framework.Data
{
	/// <summary>
	/// Represents an observable collection of entities.
	/// </summary>
	public class EntityList : ObservableCollection<Entity>
	{
		/// <summary>
		/// Constructs a new entity list.
		/// </summary>
		public EntityList() {}

		/// <summary>
		/// Constructs a new entity list from the given entities.
		/// </summary>
		/// <param name="entities">The initial set of entities in the list.</param>
		public EntityList(params Entity[] entities)
		{
			foreach (var entity in entities)
				Add(entity);
		}

		/// <summary>
		/// Adds a range of entities to the list.
		/// </summary>
		/// <param name="entities">The entities to be added.</param>
		public void AddRange(IEnumerable entities)
		{
			foreach (Entity entity in entities)
				Add(entity);
		}

		/// <summary>
		/// Finds an entity in the list by its ID.
		/// </summary>
		/// <param name="entityTypeName">The type of the entity to find.</param>
		/// <param name="iD">The ID of the entity to find.</param>
		/// <returns>The entity with the given ID, if it is in the list, null otherwise.</returns>
		public Entity FindByID(string entityTypeName, EntityID iD)
		{
			return this.FirstOrDefault(AEntity => (AEntity.EntityTypeName == entityTypeName) && (AEntity.ID == iD));
		}

		/// <summary>
		/// Replaces the contents of the list with the given set of entities.
		/// </summary>
		/// <param name="entities">The entities to replace the existing contents of the list.</param>
		/// <remarks>
		/// This method is equivalent to clearing the list and then adding the given entities.
		/// </remarks>
		public void Replace(IEnumerable entities)
		{
			Clear();
			foreach (Entity entity in entities)
				Add(entity);
		}
	}
}